<div class="page-content">
    <div class="alert alert-primary">您正在向模板:“{{pagedata.title}}” 添加数据<br>Tips:主键自动填充只适用于以下格式：00000001(纯数字)、C00000001(字母开头数字结尾)、C000SDD00001(字母数字混合，以数字结尾)，末尾连续数字不能超过16位，其他格式请手动填写或者Excel导入。</div>
        <div class="form-group">
            <div class="col-sm-12">
                <table class="table">
                    <thead>
                    <tr>
                        <th ng-repeat="field in pagedata.fields">{{field.title}} ({{field.kw}}) </th>
                        <th style="width: 50px;">操作</th>
                    </tr>
                    </thead>
                    <tbody id="type-items">
						<tr ng-repeat="item in pagedata.item.list">
                            <td ng-repeat="field in pagedata.fields">
								<div class="input-group autotc" style="width:100%;">
									<input type="text" ng-model="item[field.kw]" class="form-control count valid" placeholder="请填写 {{field.title}}">
									<div class="input-group-btn" ng-show="field.kw=='key'">
										<a class="btn btn-default btn-sm btn-add-type" style="float: right;" href="javascript:;" ng-click="pagedata.set_key(item.key,$index)">自动填充 <i class="fa fa-angle-double-down"></i></a>
									</div>
								</div>
							</td>
          
							<td>
								<a class="btn btn-default btn-op btn-operation" href="javascript:;" title="删除" ng-click="pagedata.item.list.splice($index,1)">
									<span>
										删除
									</span>
								</a>
							</td>
						</tr>
					</tbody>
                </table>
            </div>
		</div>
        <div class="form-group">
            <div class="col-sm-7">
                <div class="input-group ">
                    <span class="input-group-addon">增加</span>
                    <input type="text" ng-model="pagedata.addnum" class="form-control valid" style="padding:0px; text-align: center;">
                    <span class="input-group-addon">条数据</span>
                    <div class="input-group-btn" style="margin-left: 10px;">
                        <span class="btn btn-default btn-add-type2" style="margin-right: 10px;" ng-click="pagedata.adddata(pagedata.addnum);"><i class="fa fa-plus"></i> 确认增加</span>
						<a class="btn btn-primary" href="javascript:;" style="margin-right: 10px;" ng-click="pagedata.exceladd()"><i class="fa fa-plus" title=""></i> Excel导入</a>
						<a class="btn btn-primary" href="javascript:;" style="margin-right: 10px;" ng-click="pagedata.txtadd()"><i class="fa fa-file" title=""></i> txt文本导入(一行一条)</a>
                        <a class="btn btn-primary" ng-href="{{pagedata.template_url}}" style="margin-right: 10px;"><i class="fa fa-download" title=""></i> 下载Excel模板文件</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-12">
                <button class="btn btn-primary" ng-click="post(pagedata.item,'data_post')">保存数据</button>
                <a class="btn btn-default " href="javascript:;" ng-click="access_json('//data_list/id='+pagedata.item.id)" style="margin-left: 10px;"><i class="fa fa-list" title=""></i> 查看已有数据</a>
                <a class="btn btn-default" href="javascript:;" ng-click="access_json('list')">返回列表</a>
            </div>
        </div>
</div>
<script>
//添加数据
scope.pagedata.adddata=function(num){
	console.log('开始增加数据'+num+'条');
	for(var i=0;i<num;i++){
		scope.pagedata.item.list.push({});
	}
}
//添加数据
scope.pagedata.exceladd=function(){
	scope.tool.up_file('other',function(res){
		scope.post_json('data_add_excel',{'file':res,'id':scope.pagedata.item.id});
	});
}
//添加数据
scope.pagedata.txtadd=function(){
	console.log('3232323');
	scope.tool.up_file('other',function(res){
		console.log(res);
		scope.post_json('data_add_txt',{'file':res,'id':scope.pagedata.item.id});
	});
}
//向下填充
scope.pagedata.set_key=function(value,index){
	if(!value){
		return false;
	}
	var i;
	var number=[false,true,true,true,true,true,true,true,true,true];
	var start_index=false;var end_index=value.length-1;
	for(i=0;i<value.length;i++){
		if(!start_index){
			if(number[value[i]]){
				start_index=i;
			}
		}else{
			if(number[value[i]]!=undefined){
				end_index=i;
			}else{
				break;
			}
		}
	}
	if(!start_index){
		scope.message('填充值不合法，示例：c001','error');
	}
	end_index+=1;
	var length=scope.pagedata.item.list.length;
	var start=parseInt(value.slice(start_index,end_index));
	var pre=value.slice(0,start_index);
	if(pre){
		while(pre[pre.length-1]=='0'){
			pre=pre.slice(0,-1);
		}
	}
	for(i=index+1;i<length;i++){
		start++;
		scope.pagedata.item.list[i].key=pre+String(start);
	}
	return true;
}
</script>